﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection;
using NHibernate;
using FluentNHibernate.Cfg;
using NHibernate.Cfg;
using FluentNHibernate.Cfg.Db;
using Domain.Entity;
using System.Configuration;

namespace DataAccess.NHibernateHelper
{
    /// <summary>
    /// Uses Xml-configuration for setup-config and for mappings is Fluent-NHibernate used.
    /// </summary>
    public class FluentNHibContext
        : NHibContext
    {
        private const string DB_CONNECTION_STRING = "DB_PRODUCT_MANAGEMENT";

        /// <summary>
        /// Initializes a new instance of the <see cref="FluentNHibContext"/> class.
        /// </summary>
        /// <param name="assemblyWithMappings">The assembly containing the Fluent mappings.</param>
        public FluentNHibContext(Assembly assemblyWithMappings)
        {
            SessionFactory = CreateSessionFactory(assemblyWithMappings);
        }

       
        /// <summary>
        /// Creates and returns a session factory.
        /// </summary>
        /// <param name="assemblyWithMappings">The assembly containing the Fluent mappings.</param>
        /// <returns></returns>
        private ISessionFactory CreateSessionFactory(Assembly assemblyWithMappings)
        {
            MsSqlConfiguration msSqlConfg = MsSqlConfiguration.MsSql2008
                                  .ConnectionString(ConfigurationManager.ConnectionStrings[DB_CONNECTION_STRING].ToString()).ShowSql();
            FluentConfiguration flentConf = Fluently.Configure().Database(msSqlConfg);//.ExposeConfiguration(x => x.SetProperty("connection.release_mode", "on_close"));
            flentConf = flentConf.Mappings(m => m.FluentMappings.AddFromAssemblyOf<BookingType>());
            flentConf = flentConf.Mappings(m => m.FluentMappings.AddFromAssemblyOf<Country>());
            flentConf = flentConf.Mappings(m => m.FluentMappings.AddFromAssemblyOf<Customer>());
            flentConf = flentConf.Mappings(m => m.FluentMappings.AddFromAssemblyOf<JobBooking>());
            flentConf = flentConf.Mappings(m => m.FluentMappings.AddFromAssemblyOf<PassengerDetails>());
            flentConf = flentConf.Mappings(m => m.FluentMappings.AddFromAssemblyOf<PaymentHistory>());
            flentConf = flentConf.Mappings(m => m.FluentMappings.AddFromAssemblyOf<PaymentType>());
            flentConf = flentConf.Mappings(m => m.FluentMappings.AddFromAssemblyOf<Role>());
            flentConf = flentConf.Mappings(m => m.FluentMappings.AddFromAssemblyOf<Staff>());
            flentConf = flentConf.Mappings(m => m.FluentMappings.AddFromAssemblyOf<Status>());
            flentConf = flentConf.Mappings(m => m.FluentMappings.AddFromAssemblyOf<SystemUser>());
            flentConf = flentConf.Mappings(m => m.FluentMappings.AddFromAssemblyOf<Title>());
            return flentConf.BuildSessionFactory();

            //var cfg = new Configuration();
            //cfg = cfg.Configure();

            //return Fluently.Configure(cfg)
            //    .Mappings(m =>
            //              m.FluentMappings.AddFromAssembly(assemblyWithMappings))
            //    .BuildSessionFactory();

            //return cfg.BuildSessionFactory();
        }
    }

}
